package com.cyt.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.cyt.entity.SPFeiLei;
import com.cyt.entity.XingWenFengLei;
import com.cyt.util.DBManager;
//产品分类 基础
public class SPFeiLeiDaoImpl {
	public List<SPFeiLei> getSPFeiLei() {
		List<SPFeiLei> list = new ArrayList<SPFeiLei>();
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			// 3.sql语句
			String sql = "select * FROM spfenglei";
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) {
				int sPFengLeiId = rs.getInt("sPFengLeiId");
				String sPTuPian = rs.getString("sPTuPian");
				String sPFengLeiName = rs.getString("sPFengLeiName");
				SPFeiLei spfl = new SPFeiLei(sPFengLeiId, sPTuPian, sPFengLeiName);// 创建用户对象
				list.add(spfl);// 把user对象存入list中
			}
			return list;// 正常情况返回集合

		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	
	/**
	 * 分页查询
	 * @return
	 */
	public List<SPFeiLei> getSPFeiLeiPage(int page, int size) {
		List<SPFeiLei> list = new ArrayList<SPFeiLei>();
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			// 3.sql语句
			String sql = "select * FROM spfenglei LIMIT "+(page-1)*size+","+size;
			System.out.println(sql);
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) {
				int sPFengLeiId = rs.getInt("sPFengLeiId");
				String sPTuPian = rs.getString("sPTuPian");
				String sPFengLeiName = rs.getString("sPFengLeiName");
				SPFeiLei spfl = new SPFeiLei(sPFengLeiId, sPTuPian, sPFengLeiName);// 创建用户对象
				list.add(spfl);// 把user对象存入list中
			}
			return list;// 正常情况返回集合

		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	//产品分类上面的单个分类
	public List<SPFeiLei> getSPFeiLeiTwo(int cPFLId) {
		List<SPFeiLei> list = new ArrayList<SPFeiLei>();
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			// 3.sql语句
			String sql = "select * FROM spfenglei WHERE sPFengLeiId="+cPFLId;
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) {
				int sPFengLeiId = rs.getInt("sPFengLeiId");
				String sPTuPian = rs.getString("sPTuPian");
				String sPFengLeiName = rs.getString("sPFengLeiName");
				SPFeiLei spfl = new SPFeiLei(sPFengLeiId, sPTuPian, sPFengLeiName);// 创建用户对象
				list.add(spfl);// 把user对象存入list中
			}
			return list;// 正常情况返回集合

		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	/**
	 * 根据id删除产品分类 
	 * @param cId要删除的产品分类的id
	 * @return  返回数据库受影响行数
	 */
	public int shanChuChanPinFL(int cId) {
		return DBManager.executeUpdate("DELETE FROM spfenglei WHERE sPFengLeiId="+cId);
	}
	
	/**
	 * 添加产品系列
	 * @param spf 产品系列集合
	 * @return 返回数据库受影响行数
	 */
	public int addChanPinFL(SPFeiLei spf ) {
		return DBManager.executeUpdate("INSERT into spfenglei VALUES(null,'"+spf.getsPTuPian()+"','"+spf.getsPFengLeiName()+"')");
	}
	
	
	/**
	 * 查询产品系列的名字,以供添加时比对数据库是否存在此类别
	 * @param cpname
	 * @return
	 */
	public List<SPFeiLei> getCPFLName(String cpname){
		List<SPFeiLei> list = new ArrayList<SPFeiLei>();
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			// 3.sql语句
			String sql = "SELECT * FROM spfenglei WHERE sPFengLeiName='"+cpname+"'";
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) {
				int sPFengLeiId = rs.getInt("sPFengLeiId");
				String sPTuPian = rs.getString("sPTuPian");
				String sPFengLeiName = rs.getString("sPFengLeiName");
				SPFeiLei spfl = new SPFeiLei(sPFengLeiId, sPTuPian, sPFengLeiName);
				list.add(spfl);
			}
			return list;// 正常情况返回集合

		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	
	
	/**
	 * 根据id查产品系列信息,以供修改的数据回显
	 * @param spflId
	 * @return
	 */
	public SPFeiLei chaSPFeiLeiId(int spflId) {
		SPFeiLei spfl=null;
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			String sql = "  SELECT * FROM spfenglei WHERE sPFengLeiId="+spflId;
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int sPFengLeiId = rs.getInt("sPFengLeiId");
				String sPTuPian = rs.getString("sPTuPian");
				String sPFengLeiName = rs.getString("sPFengLeiName");
				spfl = new SPFeiLei(sPFengLeiId, sPTuPian, sPFengLeiName);
				
			}
			return spfl;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	/**
	 * 修改商品系列  根据id
	 * @param spfl
	 * @return
	 */
	public int xiuGaiSPFeiLei(SPFeiLei spfl ) {
		String sql="UPDATE spfenglei set sPTuPian='"+spfl.getsPTuPian()+"',sPFengLeiName='"+spfl.getsPFengLeiName()+"' where sPFengLeiId="+spfl.getsPFengLeiId();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
	
	/**
	 * 通过id主键删除留言
	 * 批量删除
	 * @param ids
	 * @return
	 */
	public int shanChanPinFLS(String ids) {
		String sql = "delete from spfenglei where sPFengLeiId in("+ids+")";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
}
